<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="mt30" :size="formSize" status-icon >
        <el-form-item label="审核意见" prop="comment">
            <el-input v-model="ruleForm.comment" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
    </el-form>
            <h2 class="text-center mt30">审批流程<el-text v-if="ruleForm.type == 'delete'" type="error">(删除)</el-text></h2>
    <div class="p15">
        <div>发起人：{{ ruleForm.user.name }}　　发起时间：{{ ruleForm.created_at }}</div>
        <el-table :data="records" style="width: 100%;height: auto !important;">
            <el-table-column label="环节" width="100" align="center">
                <template #default="scope">
                    <el-text :type="scope.row.is_process == 1 ? 'primary' : 'info'">{{ scope.row.user.name }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="部门" width="120" align="center">
                <template #default="scope">
                    <div class="text-center"><el-text :type="scope.row.is_process == 1 ? 'primary' : 'info'" >{{ process[scope.row.level] }}</el-text></div>
                </template>
            </el-table-column>
            <el-table-column prop="status_text" width="100" label="结果" align="center" />
            <el-table-column prop="updated_at" width="180" label="时间" align="center" />
            <el-table-column prop="comment" label="意见" align="center" />
        </el-table>
    </div>
    <el-divider />
    <div class="text-center">
        <el-button type="success" v-if="ruleForm.is_review == false " @click="submitForm(ruleFormRef,'approved')" :loading="loading"> 通过 </el-button>
        <el-button type="error" v-if="ruleForm.is_review == false " @click="submitForm(ruleFormRef,'rejected')" :loading="loading"> 退回 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id: number
    reviewer_id:number
    application_id:number
    status: string
    comment:string
    is_review:boolean
    created_at:string
    type: string
    user:any
}
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    reviewer_id:0,
    application_id:0,
    status: '',
    comment:'/',
    is_review:false,
    created_at:'',
    type: '',
    user:{id:0,name:''}
})

const process = ref<string[]>(['','项目部','预算员','综合管理部','项目合伙人','总经理','财务','出纳'])

const records: any = ref([])

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/approve/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
        records.value = response.data.data.records
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.reviewer_id = 0
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    status: [
        { required: true, message: '请选择操作类型', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined, status: string) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            ruleForm.value.status = status
            ruleForm.value.application_id = ruleForm.value.id
            loading.value = true
            http.patch('/approve/review',ruleForm.value).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
